/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */

package com.ecarx.systemui.plugin.ui.navigationbar;

import android.content.Context;
import android.content.res.Configuration;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.content.res.AppCompatResources;

import com.ecarx.systemui.plugin.AppConstants;
import com.ecarx.systemui.plugin.R;
import com.ecarx.systemui.plugin.controller.hvac.SeatSignalController;
import com.ecarx.systemui.plugin.ui.dialog.DriverSeatWheelDialog;
import com.ecarx.systemui.plugin.ui.dialog.base.DialogManager;
import com.ecarx.systemui.plugin.model.listener.NoDoubleClickListener;
import com.ecarx.systemui.plugin.model.listener.SimpleSeatDialogCallBack;
import com.ecarx.systemui.plugin.model.listener.SimpleSeatFuncNotifyViewListener;
import com.ecarx.systemui.plugin.utils.LogUtils;
import com.ecarx.xui.adaptapi.FunctionStatus;

public class NavigationBarLeftSeatView extends FrameLayout {

    private static final String TAG = "NavigationBarLeftSeatViewTag";

    private SeatSignalController seatSignalController;

    private ImageView ivWheel;
    private ImageView ivWheel0;
    private ImageView ivSeatHeat;
    private ImageView ivSeatVent;

    public NavigationBarLeftSeatView(@NonNull Context context) {
        this(context, null);
    }

    public NavigationBarLeftSeatView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NavigationBarLeftSeatView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private final SimpleSeatDialogCallBack seatDialogCallBack = new SimpleSeatDialogCallBack() {
        @Override
        public void adjustWheelHeat() {
            if (seatSignalController != null) {
                seatSignalController.adjustWheelHeat();
            }
        }

        @Override
        public void adjustDriverSeatHeat() {
            if (seatSignalController != null) {
                seatSignalController.adjustDriverSeatHeat();
            }
        }

        @Override
        public void adjustDriverSeatVent() {
            if (seatSignalController != null) {
                seatSignalController.adjustDriverSeatVent();
            }
        }
    };

    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.navigation_left_seat, this, true);
        ivWheel = findViewById(R.id.iv_wheel);
        ivWheel0 = findViewById(R.id.iv_wheel_0);
        ivSeatHeat = findViewById(R.id.iv_seat_heat);
        ivSeatVent = findViewById(R.id.iv_seat_ventilation);

        ivWheel0.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {

            }
        });

        ivSeatHeat.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                if (seatSignalController == null) {
                    return;
                }
                boolean supportDriverSeatVent = seatSignalController.isSupportDriverSeatVent();
                boolean supportWheelHeat = seatSignalController.isSupportWheelHeat();
                if (supportDriverSeatVent || supportWheelHeat) {
                    //Show seat dialog
                    int[] location = new int[2];
                    ivSeatHeat.getLocationOnScreen(location);
                    location[0] = location[0] + ivSeatHeat.getWidth() / 2;
                    int dialogWidth = DriverSeatWheelDialog.DIALOG_TOTAL_WIDTH;
                    if (!supportDriverSeatVent) {
                        dialogWidth = dialogWidth - DriverSeatWheelDialog.ICON_WITH;
                    }
                    if (!supportWheelHeat) {
                        dialogWidth = dialogWidth - DriverSeatWheelDialog.ICON_WITH;
                    }
                    location[0] = location[0] - dialogWidth / 2; //最终dialog 显示的x坐标
                    DriverSeatWheelDialog driverSeatWheelDialog = DialogManager.getDialogManager()
                            .showDriverSeatWheelDialog(getContext(), location, supportWheelHeat, true, supportDriverSeatVent);
                    driverSeatWheelDialog.setDialogCallBack(seatDialogCallBack);
                    int wheelValue = -1;
                    if (supportWheelHeat) {
                        wheelValue = seatSignalController.getWheelHeatValue();
                    }

                    int seatHeatValue = seatSignalController.getDriverSeatHeatValue();

                    int seatVentValue = -1;
                    if (supportDriverSeatVent) {
                        seatVentValue = seatSignalController.getDriverSeatVentValue();
                    }
                    driverSeatWheelDialog.updateSignalValue(wheelValue, seatHeatValue, seatVentValue);
                } else {
                    //信号仅支持座椅加热
                    seatSignalController.adjustDriverSeatHeat();
                }
            }
        });

        ivSeatVent.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                if (seatSignalController == null) {
                    return;
                }
                boolean supportDriverSeatHeat = seatSignalController.isSupportDriverSeatHeat();
                boolean supportWheelHeat = seatSignalController.isSupportWheelHeat();
                if (supportDriverSeatHeat || supportWheelHeat) {
                    //Show seat dialog
                    int[] location = new int[2];
                    ivSeatVent.getLocationOnScreen(location);
                    location[0] = location[0] + ivSeatVent.getWidth() / 2; // 点击的View中心X坐标
                    int dialogWidth = DriverSeatWheelDialog.DIALOG_TOTAL_WIDTH;
                    if (!supportDriverSeatHeat) {
                        dialogWidth = dialogWidth - DriverSeatWheelDialog.ICON_WITH;
                    }
                    if (!supportWheelHeat) {
                        dialogWidth = dialogWidth - DriverSeatWheelDialog.ICON_WITH;
                    }
                    location[0] = location[0] - dialogWidth / 2; //最终dialog 显示的x坐标
                    DriverSeatWheelDialog driverSeatWheelDialog = DialogManager.getDialogManager().showDriverSeatWheelDialog(
                            getContext(), location, supportWheelHeat, supportDriverSeatHeat, true);
                    driverSeatWheelDialog.setDialogCallBack(seatDialogCallBack);
                    int wheelValue = -1;
                    if (supportWheelHeat) {
                        wheelValue = seatSignalController.getWheelHeatValue();
                    }

                    int seatHeatValue = -1;
                    if (supportDriverSeatHeat) {
                        seatHeatValue = seatSignalController.getDriverSeatHeatValue();
                    }

                    int seatVentValue = seatSignalController.getDriverSeatVentValue();

                    driverSeatWheelDialog.updateSignalValue(wheelValue, seatHeatValue, seatVentValue);
                } else {
                    //信号仅支持座椅通风
                    seatSignalController.adjustDriverSeatVent();
                }
            }
        });
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (seatSignalController == null) {
            seatSignalController = SeatSignalController.getInstance();
        }
        seatSignalController.setSeatLeftNotifyViewListener(seatFuncNotifyListener);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (seatSignalController != null) {
            seatSignalController.setSeatLeftNotifyViewListener(null);
        }
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    protected void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (ivSeatHeat != null) {
            //ivSeatHeat.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.left_seat_heating_off));
        }
    }

    public void onSeatHeatValueChanged(Integer value) {
        LogUtils.d(TAG, "onSeatHeatValueChanged value:" + value);
        if (value == null) {
            return;
        }
        int resId = getHeatIconByHeatValue(value);
        updateIcon(ivSeatHeat, resId);
    }

    public void onSeatHeatStatusChanged(FunctionStatus status) {
        LogUtils.d(TAG, "onSeatHeatStatusChanged FuncStatus:" + status);
        updateIconStatus(ivSeatHeat, status);
    }

    public void onSeatVentValueChanged(Integer value) {
        LogUtils.d(TAG, "onSeatVentValueChanged value:" + value);
        if (value == null) {
            return;
        }
        int resId = getVentIconByVentValue(value);
        updateIcon(ivSeatVent, resId);
    }

    public void onSeatVentStatusChanged(FunctionStatus status) {
        LogUtils.d(TAG, "onSeatVentStatusChanged FuncStatus:" + status);
        updateIconStatus(ivSeatVent, status);
    }

    public void onWheelHeatValueChanged(int value) {
        LogUtils.d(TAG, "onWheelHeatValueChanged value:" + value);
        updateWheelIcon(value);
    }

    public void onWheelHeatStatusChanged(FunctionStatus status) {
        LogUtils.d(TAG, "onWheelHeatStatusChanged FuncStatus:" + status);
        updateIconStatus(ivWheel, status);
        updateIconStatus(ivWheel0, status);
    }


    private void updateIcon(ImageView imageView, @DrawableRes int resId) {
        post(() -> imageView.setImageDrawable(AppCompatResources.getDrawable(getContext(), resId)));
    }

    private void updateWheelIcon(Integer value) {
        post(() -> {
            switch (value) {
                case AppConstants.HEAT_LEVEL_0:
                    ivWheel.setImageDrawable(AppCompatResources.getDrawable(getContext(), R.drawable.wheel_heat_off));
                    ivWheel0.setImageDrawable(AppCompatResources.getDrawable(getContext(), R.drawable.wheel_heat_0));
                    break;
                case AppConstants.HEAT_LEVEL_1:
                    ivWheel.setImageDrawable(AppCompatResources.getDrawable(getContext(), R.drawable.wheel_heat_on));
                    ivWheel0.setImageDrawable(AppCompatResources.getDrawable(getContext(), R.drawable.wheel_heat_1));
                    break;
                case AppConstants.HEAT_LEVEL_2:
                    ivWheel.setImageDrawable(AppCompatResources.getDrawable(getContext(), R.drawable.wheel_heat_on));
                    ivWheel0.setImageDrawable(AppCompatResources.getDrawable(getContext(), R.drawable.wheel_heat_2));
                    break;
                case AppConstants.HEAT_LEVEL_3:
                    ivWheel.setImageDrawable(AppCompatResources.getDrawable(getContext(), R.drawable.wheel_heat_on));
                    ivWheel0.setImageDrawable(AppCompatResources.getDrawable(getContext(), R.drawable.wheel_heat_3));
                    break;
            }
        });
    }

    private int getHeatIconByHeatValue(Integer value) {
        switch (value) {
            case AppConstants.HEAT_LEVEL_0:
                return R.drawable.left_seat_heating_off;
            case AppConstants.HEAT_LEVEL_1:
                return R.drawable.left_seat_heating_1;
            case AppConstants.HEAT_LEVEL_2:
                return R.drawable.left_seat_heating_2;
            case AppConstants.HEAT_LEVEL_3:
                return R.drawable.left_seat_heating_3;
        }
        LogUtils.d(TAG, "NO SEAT HEAT VALUE value:" + value);
        return 0;
    }

    private int getVentIconByVentValue(Integer value) {
        switch (value) {
            case AppConstants.HEAT_LEVEL_0:
                return R.drawable.left_seat_ventilation_off;
            case AppConstants.HEAT_LEVEL_1:
                return R.drawable.left_seat_ventilation_1;
            case AppConstants.HEAT_LEVEL_2:
                return R.drawable.left_seat_ventilation_2;
            case AppConstants.HEAT_LEVEL_3:
                return R.drawable.left_seat_ventilation_3;
        }
        LogUtils.d(TAG, "NO SEAT Vent VALUE value:" + value);
        return 0;
    }

    private void updateIconStatus(View view, FunctionStatus status) {
        postDelayed(() -> {
            if (FunctionStatus.active == status) {
                if (view != null) {
                    view.setAlpha(1f);
                    view.setEnabled(true);
                }
            } else {
                if (view != null) {
                    view.setAlpha(0.5f);
                    view.setEnabled(false);
                }
            }
            //这里可以在统一判断下三个功能的状态，统一修改一遍
            if (seatSignalController == null) {
                return;
            }
            boolean supportDriverSeatHeat = seatSignalController.isSupportDriverSeatHeat();
            boolean supportDriverSeatVent = seatSignalController.isSupportDriverSeatVent();
            boolean supportWheelHeat = seatSignalController.isSupportWheelHeat();
            LogUtils.d(TAG, "IS Active supportDriverSeatHeat:" + supportDriverSeatHeat +
                    ", supportDriverSeatVent:" + supportDriverSeatVent +
                    ", supportWheelHeat:" + supportWheelHeat);

            if (supportDriverSeatHeat || supportDriverSeatVent) {
                ivWheel0.setVisibility(GONE);
                if (supportDriverSeatHeat && supportDriverSeatVent) {
                    int driverSeatHeatValue = seatSignalController.getDriverSeatHeatValue();
                    if(255!=driverSeatHeatValue &&
                            AppConstants.HEAT_LEVEL_0 != driverSeatHeatValue){
                        ivSeatHeat.setVisibility(VISIBLE);
                        ivSeatVent.setVisibility(GONE);
                    }else{
                        ivSeatHeat.setVisibility(GONE);
                        ivSeatVent.setVisibility(VISIBLE);
                    }
                } else {
                    if (supportDriverSeatHeat) {
                        ivSeatHeat.setVisibility(VISIBLE);
                        ivSeatVent.setVisibility(GONE);
                    } else {
                        ivSeatHeat.setVisibility(GONE);
                        ivSeatVent.setVisibility(VISIBLE);
                    }
                }
                if (supportWheelHeat) {
                    ivWheel.setVisibility(VISIBLE);
                } else {
                    ivWheel.setVisibility(GONE);
                }
            } else {
                ivSeatHeat.setVisibility(GONE);
                ivSeatVent.setVisibility(GONE);
                if (supportWheelHeat) {
                    ivWheel0.setVisibility(VISIBLE);
                    ivWheel.setVisibility(GONE);
                } else {
                    ivWheel.setVisibility(GONE);
                    ivWheel0.setVisibility(GONE);
                }
            }
            if (ivSeatHeat.getVisibility() == GONE && ivSeatVent.getVisibility() == GONE &&
                    ivWheel.getVisibility() == GONE && ivWheel0.getVisibility() == GONE) {
                setVisibility(GONE);
            } else {
                setVisibility(VISIBLE);
            }
        }, 10);
    }


    //------------------------------------信号回调开始------------------------------------------------
    private final SimpleSeatFuncNotifyViewListener seatFuncNotifyListener =
            new SimpleSeatFuncNotifyViewListener() {
                /**
                 * 通知主驾驶注意加热值变化
                 *
                 * @param value 座椅加热值
                 */
                @Override
                public void notifyDriverSeatHeatChanged(int value, FunctionStatus functionStatus) {
                    super.notifyDriverSeatHeatChanged(value, functionStatus);
                    onSeatHeatValueChanged(value);
                    onSeatHeatStatusChanged(functionStatus);
                }

                /**
                 * 通知主驾驶座椅通风值变化
                 *
                 * @param value 座椅通风值
                 */
                @Override
                public void notifyDriverSeatVentChanged(int value, FunctionStatus functionStatus) {
                    super.notifyDriverSeatVentChanged(value, functionStatus);
                    onSeatVentValueChanged(value);
                    onSeatVentStatusChanged(functionStatus);
                }

                /**
                 * 通知主方向盘加热值变化
                 *
                 * @param value 向盘加热值
                 */
                @Override
                public void notifyWheelHeatChanged(int value, FunctionStatus functionStatus) {
                    super.notifyWheelHeatChanged(value, functionStatus);
                    onWheelHeatValueChanged(value);
                    onWheelHeatStatusChanged(functionStatus);
                }
            };

    //------------------------------------信号回调结束------------------------------------------------
}
